﻿using System.IO.Compression;
using System.Runtime.InteropServices;


namespace CodeBackup.Code
{
    internal class zlb
    {

        [DllImport("extractzlb.dll", EntryPoint = "ExtractBackupFile", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        private static extern bool ExtractBackupFile(string BackupFile, string ExtractPath);



        public bool ExtractFile(string SourceFile, string ExtFolder)
        {
            return ExtractBackupFile(SourceFile, ExtFolder);
        }

        public string ZlbToZip(string SourceFile)
        {
            string directory = Path.GetDirectoryName(SourceFile);
            string temp = Guid.NewGuid().ToString();
            string temFolder = Path.Combine(directory, temp);
            Directory.CreateDirectory(temFolder);

            try
            {

                ExtractBackupFile(SourceFile, temFolder);
                string FileName = Path.GetFileNameWithoutExtension(SourceFile) + ".zip";
                string zipFileName = Path.Combine(directory, FileName);
                if (File.Exists(zipFileName))
                {
                    File.Delete(zipFileName);
                }
                ZipFile.CreateFromDirectory(temFolder, zipFileName);

                return zipFileName;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return string.Empty;
            }
            finally
            {
                if (Directory.Exists(temFolder))
                {
                    Directory.Delete(temFolder, true);
                }
            }

        }
    }
}
